﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurriculumnManage.Helper;
using CurriculumnManage.DBAccess;

namespace CurriculumnManage
{
    public partial class PublicForm : Form
    {
        public PublicForm()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CommonFunction.CheckYear(this.txtYear)) return;
            //if (!CommonFunction.CheckGrade(this.cmbGrade)) return;
            //if (!chkAll.Checked && !chk1.Checked && !chk2.Checked && !chk3.Checked && !chk4.Checked)
            //{
            //    CommonFunction.ShowMsg("学年を選択してください。");
            //    return;
            //}
            try
            {
                //if (chkAll.Checked)
                //{
                    UpdatePublicDate(1, MyUtils.ParseToDateTime(this.dtPublicS1.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE1.Text, DateTime.Now));
                    UpdatePublicDate(2, MyUtils.ParseToDateTime(this.dtPublicS2.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE2.Text, DateTime.Now));
                    UpdatePublicDate(3, MyUtils.ParseToDateTime(this.dtPublicS3.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE3.Text, DateTime.Now));
                    UpdatePublicDate(4, MyUtils.ParseToDateTime(this.dtPublicS4.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE4.Text, DateTime.Now));
                //}
                //else
                //{
                //    if (chk1.Checked)
                //    {
                //        UpdatePublicDate(1, MyUtils.ParseToDateTime(this.dtPublicS1.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE1.Text, DateTime.Now));
                //    }
                //    if (chk2.Checked)
                //    {
                //        UpdatePublicDate(2, MyUtils.ParseToDateTime(this.dtPublicS2.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE2.Text, DateTime.Now));
                //    }
                //    if (chk3.Checked)
                //    {
                //        UpdatePublicDate(3, MyUtils.ParseToDateTime(this.dtPublicS3.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE3.Text, DateTime.Now));
                //    }
                //    if (chk4.Checked)
                //    {
                //        UpdatePublicDate(4, MyUtils.ParseToDateTime(this.dtPublicS4.Text, DateTime.Now), MyUtils.ParseToDateTime(this.dtPublicE4.Text, DateTime.Now));
                //    }
                //}
                MessageBox.Show("公開期間の設定は完了しました。");
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("PublicForm:btnSave_Click", ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdatePublicDate(int index, DateTime start, DateTime end)
        {
            try
            {
                using (DBContext db = new DBContext())
                {
                    object[] paras = new object[4];
                    paras[0] = MyUtils.ParseToInt(this.txtYear.Text, 0);
                    paras[1] = index;
                    paras[2] = start;
                    paras[3] = end;
                    SqlResult result = db.CallProcedure("usp_tbSTRPublic_add_edit", paras);
                }
            }
            catch (Exception ex)
            {
                MyLog.TraceInstance.TraceInformation(DateTime.Now.ToString() + ":" + "PublicForm:btnSave_Click" + ":" + ex.Message);
            }
        }

        private void PublicForm_Load(object sender, EventArgs e)
        {
            CommonFunction.SetYearGrade(this.txtYear);
            ShowPublicDate();
        }

        private void ShowPublicDate()
        {
            try
            {
                this.dtPublicS.Text = "";
                this.dtPublicE.Text = "";
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_tbSTRPublic_get", 1, MyUtils.ParseToInt(this.txtYear.Text, 0));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        this.dtPublicS1.Text = dt.Rows[0]["dtPublicS"].ToString();
                        this.dtPublicE1.Text = dt.Rows[0]["dtPublicE"].ToString();
                    }
                    result = db.CallProcedure("usp_tbSTRPublic_get", 2, MyUtils.ParseToInt(this.txtYear.Text, 0));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        this.dtPublicS2.Text = dt.Rows[0]["dtPublicS"].ToString();
                        this.dtPublicE2.Text = dt.Rows[0]["dtPublicE"].ToString();
                    }
                    result = db.CallProcedure("usp_tbSTRPublic_get", 3, MyUtils.ParseToInt(this.txtYear.Text, 0));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        this.dtPublicS3.Text = dt.Rows[0]["dtPublicS"].ToString();
                        this.dtPublicE3.Text = dt.Rows[0]["dtPublicE"].ToString();
                    }
                    result = db.CallProcedure("usp_tbSTRPublic_get", 4, MyUtils.ParseToInt(this.txtYear.Text, 0));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        this.dtPublicS4.Text = dt.Rows[0]["dtPublicS"].ToString();
                        this.dtPublicE4.Text = dt.Rows[0]["dtPublicE"].ToString();
                    }
                    if (dtPublicS1.Text == dtPublicS2.Text && dtPublicS3.Text == dtPublicS4.Text && dtPublicS2.Text == dtPublicS3.Text)
                    {
                        dtPublicS.Text = dtPublicS1.Text;
                    }
                    if (dtPublicE1.Text == dtPublicE2.Text && dtPublicE3.Text == dtPublicE4.Text && dtPublicE2.Text == dtPublicE3.Text)
                    {
                        dtPublicE.Text = dtPublicE1.Text;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("PublicForm.cmbGrade_SelectedIndexChanged", ex.Message);
            }
        }

        private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowPublicDate();
        }

        private void btnSetAll_Click(object sender, EventArgs e)
        {
            this.dtPublicE1.Text = this.dtPublicE.Text;
            this.dtPublicE2.Text = this.dtPublicE.Text;
            this.dtPublicE3.Text = this.dtPublicE.Text;
            this.dtPublicE4.Text = this.dtPublicE.Text;
            this.dtPublicS1.Text = this.dtPublicS.Text;
            this.dtPublicS2.Text = this.dtPublicS.Text;
            this.dtPublicS3.Text = this.dtPublicS.Text;
            this.dtPublicS4.Text = this.dtPublicS.Text;           
        }
    }
}
